package com.ia2014.tp.portal2.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ia2014.tp.portal2.facades.FacadeIntegracion;
import com.ia2014.tp.portal2.vo.UsuarioVO;
import com.ia2014.tp.portal2.vo.VentaVO;

@WebServlet("/listVentas")
public class ListVentasServlet extends AbstractServlet{
	private static final long serialVersionUID = 5912397760551010545L;
	
	@EJB
	private FacadeIntegracion facade;
	
	
	public ListVentasServlet(){
		super();
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doGet(req, resp);
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.comprobarSession(req, resp);
		this.process(req, resp);
	}

	
	private void process(HttpServletRequest req, HttpServletResponse resp) 
			throws ServletException, IOException{
		//Levanto la session y saco los parametros de configuracion
		HttpSession session  = req.getSession(false);
		String redirect = "listVentas.jsp";
		UsuarioVO usuario = this.getUsuarioVOFromSession(session);
		usuario = this.facade.loadUsuario(usuario);
		this.setUsuarioVOToSession(session, usuario);
		List<VentaVO> listVentas = this.facade.getListaVentasByUsuario(usuario);
		if (listVentas == null){
			listVentas = new ArrayList<VentaVO>();
		}
		this.setVentasVOToSession(session, listVentas);
		resp.sendRedirect(redirect);
	}	
}
